home *** CD-ROM | disk | FTP | other *** search
- //
- // downloadmanager.js
- // firefox
- //
- // Created by Zak on 2008-06-17.
- // Contributor Brian King (http://briks.si)
- // Copyright 2008-2009 Ant.com. All rights reserved.
- //
-
- var AntDownloadManager =
- {
- /**
- * Regex for extracting the filetype from the ContentType
- * /!\ If you change it change the AntPlayer.movieFileMatch RegExp too.
- */
- extFromContentType: /(flv|mp4|asf)/i,
- defaultExtension: 'flv',
-
- /**
- * start
- */
- init: function ()
- {
- },
-
- /**
- * stop
- */
- destroy: function ()
- {
- },
-
- /**
- * Return the file extention from the contentType
- */
- getExtension: function (contentType)
- {
- var m = contentType.match(this.extFromContentType);
-
- if (m != null)
- {
- return m[1].toLowerCase();
- }
- return this.defaultExtension;
- },
-
- /**
- * return the movies destination folder and create it if it doesn't exist
- */
- getDestFolder: function ()
- {
- var flvdir;
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
-
- try
- {
- flvdir = AntPrefs.flvDir;
- file.initWithPath(flvdir);
- if (!file.exists())
- file.create(AntLib.CI("nsIFile").DIRECTORY_TYPE, 0777);
- if (!file.isDirectory())
- {
- alert(AntLang.getString("AntDownloadManager.InvalidDestinationFolder"));
- return null;
- }
- }
- catch (e) { alert(AntLang.getString("AntDownloadManager.NoFlvDir")); }
-
- return file;
- },
-
- /**
- * public function to download a flv from the toolbar
- * @param origin the origin web site where the movie is from ex youtube
- * @param url where the flv is
- * @param name of the video
- * @param doNotOpen if true the function won't open the firefox download manager
- * @return destFile.path if available else false
- */
- downloadFlv: function (flvLink, doNotOpen)
- {
- flvLink.name = AntLib.sanitize(flvLink.name);
- flvLink.origin = AntLib.sanitize(flvLink.origin);
-
- var destFile = this.getDestFolder();
- if (!destFile)
- return false;
-
- var ext = this.getExtension(flvLink.contentType);
-
- destFile.append(flvLink.origin + "." + flvLink.name + "." + ext);
- while (destFile.exists())
- {
- destFile = this.getDestFolder();
- flvLink.incrementFlvName();
- destFile.append(flvLink.origin + "." + flvLink.name + "." + ext);
- }
- destFile.create(AntLib.CI("nsIFile").NORMAL_FILE_TYPE, 0777);
- this.download(flvLink, destFile);
- if (!doNotOpen)
- this.openDownloadWindow();
- return destFile.path;
- },
-
- /**
- * openDownloadWindow opens the firefox downloadManager
- */
- openDownloadWindow: function ()
- {
- AntLib.openWindow("Download:Manager", "chrome://mozapps/content/downloads/downloads.xul", {});
- },
-
-
- /**
- * Checks if the URL is in the cache
- */
- isInCache: function(url)
- {
- var cacheService = Components.classes['@mozilla.org/network/cache-service;1']
- .getService(Components.interfaces.nsICacheService);
-
- var httpCacheSession = cacheService.createSession("HTTP", Components.interfaces.nsICache.STORE_ANYWHERE, true);
- httpCacheSession.doomEntriesIfExpired = false;
-
- try {
- var cacheEntryDescriptor = httpCacheSession.openCacheEntry(url, Components.interfaces.nsICacheEntryDescriptor, false);
- }
- catch (e)
- {
- if ( e.name == "NS_ERROR_CACHE_WAIT_FOR_VALIDATION" )
- return false;
-
- if ( e.name == "NS_ERROR_CACHE_KEY_NOT_FOUND" )
- return false;
-
- return false;
- }
-
- AntLib.toLog("isInCache returning: " + cacheEntryDescriptor);
- return cacheEntryDescriptor != null;
- },
-
-
- /**
- * low level function to download a flv
- * @param flvlink AntFlvLink object
- * @param dest destination file type nsILocalFile
- */
- download: function (flvlink, dest)
- {
- try
- {
- var ioService = AntLib.CCSV("@mozilla.org/network/io-service;1", "nsIIOService");
- var s_uri = ioService.newURI(flvlink.url, null, null);
- var t_uri = ioService.newFileURI(dest);
- var nsIWBP = AntLib.CI("nsIWebBrowserPersist");
- var browserPersist = AntLib.CCIN('@mozilla.org/embedding/browser/nsWebBrowserPersist;1', "nsIWebBrowserPersist");
- var dlMgr = AntLib.CCSV('@mozilla.org/download-manager;1', "nsIDownloadManager");
- var flags =
- nsIWBP.PERSIST_FLAGS_NO_CONVERSION |
- nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
- nsIWBP.PERSIST_FLAGS_CLEANUP_ON_FAILURE;
-
- if ( this.isInCache(flvlink.url) )
- flags |= nsIWBP.PERSIST_FLAGS_FROM_CACHE;
- else
- flags |= nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
-
- browserPersist.persistFlags = flags;
-
- /**
- * Add the download to the downloader window
- */
- var ret = dlMgr.addDownload(0, s_uri, t_uri, flvlink.name.replace(/_/g, " "), null, null, null, browserPersist, browserPersist);
- //dlMgr.addListener(AntDownloadListener);
- AntPrefs.flvToPlay = dest.path;
- browserPersist.progressListener = ret;
-
- /**
- * Start downloading
- */
- // AntLib.toLog("flvlink.header = " + flvlink.header);
- browserPersist.saveURI(s_uri, null, null, null, flvlink.header, t_uri);
-
- try {
- AntDownloadManager.sendDownloadInfo(flvlink);
- }
- catch (e) {
- AntLib.toLog("AntDownloadManager: sendDownloadInfo error: " + e);
- }
- }
- catch (e)
- {
- alert(AntLang.getString("AntDownloadManager.DownloadError"));
- AntLib.toLog("AntDownloadManager: Download error: " + e);
- }
- },
-
- /**
- * Send anonymous download stats
- * @param flvlink AntFlvLink object
- */
- sendDownloadInfo: function (flvlink)
- {
- if (!AntPrefs.canSendStats)
- return;
-
- var body = "url=" + AntLib.uriEncode(gBrowser.selectedBrowser.contentDocument.location.href) +
- "&ref=" + AntLib.uriEncode(gBrowser.selectedBrowser.contentDocument.referrer) +
- "&stream=" + AntLib.uriEncode(flvlink.url) +
- "&type=" + AntLib.uriEncode(flvlink.contentType) +
- "&length=" + AntLib.uriEncode(flvlink.contentLength) +
- "&uid=" + AntLib.uriEncode(AntRankService.UUID);
-
- var httpRequest = new XMLHttpRequest();
-
- httpRequest.open("POST", "http://report.ant.com/download/", true);
- httpRequest.onload = function () { /* Async Query, do nothing with the reply */ };
- httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- httpRequest.send(body);
-
- AntLib.toLog("Sending download report to Ant, data: " + body);
- }
-
- }
-